package cn.iocoder.yudao.module.famous.doctor.service.video;

import org.springframework.stereotype.Service;
import javax.annotation.Resource;
import org.springframework.validation.annotation.Validated;

import java.util.*;
import cn.iocoder.yudao.module.famous.doctor.controller.admin.video.vo.*;
import cn.iocoder.yudao.module.famous.doctor.dal.dataobject.video.FamVideoDO;
import cn.iocoder.yudao.framework.common.pojo.PageResult;

import cn.iocoder.yudao.module.famous.doctor.convert.video.FamVideoConvert;
import cn.iocoder.yudao.module.famous.doctor.dal.mysql.video.FamVideoMapper;

import static cn.iocoder.yudao.framework.common.exception.util.ServiceExceptionUtil.exception;
import static cn.iocoder.yudao.module.famous.doctor.enums.ErrorCodeConstants.*;

import cn.hutool.core.collection.CollUtil;
import cn.hutool.core.collection.ListUtil;

/**
 * 视频管理 Service 实现类
 *
 * @author 档案
 */
@Service
@Validated
public class FamVideoServiceImpl implements FamVideoService {

    @Resource
    private FamVideoMapper famVideoMapper;

    @Override
    public Long createFamVideo(FamVideoCreateReqVO createReqVO) {
        // 插入
        FamVideoDO famVideo = FamVideoConvert.INSTANCE.convert(createReqVO);
        famVideoMapper.insert(famVideo);
        // 返回
        return famVideo.getId();
    }

    @Override
    public void updateFamVideo(FamVideoUpdateReqVO updateReqVO) {
        // 校验存在
        validateFamVideoExists(updateReqVO.getId());
        // 更新
        FamVideoDO updateObj = FamVideoConvert.INSTANCE.convert(updateReqVO);
        famVideoMapper.updateById(updateObj);
    }

    @Override
    public void deleteFamVideo(Long id) {
        // 校验存在
        validateFamVideoExists(id);
        // 删除
        famVideoMapper.deleteById(id);
    }

    private void validateFamVideoExists(Long id) {
        if (famVideoMapper.selectById(id) == null) {
            throw exception(FAM_VIDEO_NOT_EXISTS);
        }
    }

    @Override
    public FamVideoDO getFamVideo(Long id) {
        return famVideoMapper.selectById(id);
    }

    @Override
    public List<FamVideoDO> getFamVideoList(Collection<Long> ids) {
        if (CollUtil.isEmpty(ids)) {
            return ListUtil.empty();
        }
        return famVideoMapper.selectBatchIds(ids);
    }

    @Override
    public PageResult<FamVideoDO> getFamVideoPage(FamVideoPageReqVO pageReqVO) {
        return famVideoMapper.selectPage(pageReqVO);
    }

    @Override
    public List<FamVideoDO> getFamVideoList(FamVideoExportReqVO exportReqVO) {
        return famVideoMapper.selectList(exportReqVO);
    }

}
